package com.yunhe.sms.service.system.impl;


import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.dto.TreeNode;
import com.yunhe.sms.dao.system.PermissionDao;
import com.yunhe.sms.dao.system.impl.PermissionDaoImpl;
import com.yunhe.sms.entity.system.Permission;
import com.yunhe.sms.service.system.PermissionService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
public class PermissionServiceImpl implements PermissionService{
    private PermissionDao permissionDao = new PermissionDaoImpl();
    @Override
    public void save(Permission permission) {
        permissionDao.save(permission);
    }

    @Override
    public void delete(Long id) {
        permissionDao.delete(id);
    }

    @Override
    public void deleteBatch(String ids) {
        if(null != ids && !ids.isEmpty()){
            String[] idsArr = ids.split(",");
            for(String id : idsArr){
                permissionDao.delete(Long.valueOf(id));
            }
        }
    }

    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public Permission find(Long id) {
        Permission permission = permissionDao.find(id);
        return permission;
    }

    @Override
    public List<Permission> findAll() {
        List<Permission> permissions = permissionDao.findAll();
        return permissions;
    }

    @Override
    public List<TreeNode> findTreeNodes(Long roleId){
        List<TreeNode> treeNodes = new ArrayList<>();
        List<Permission> allPermission = this.findAll();
        List<Long> rolePermiIds = this.findIdListByRole(roleId);
        if(null != allPermission){
            for(Permission p : allPermission){
                TreeNode treeNode = new TreeNode(p.getId(),p.getParentId(),p.getName());
                if(rolePermiIds.contains(p.getId())){
                    treeNode.setChecked(Boolean.TRUE);
                }
                treeNodes.add(treeNode);
            }
        }

        return treeNodes;
    }
    @Override
    public List<Permission> findByUser(Long userId) {
        List<Permission> permissions = permissionDao.findByUser(userId);
        return permissions;
    }

    @Override
    public List<Long> findIdListByRole(Long roleId) {
        List<Long> idList = permissionDao.findIdListByRole(roleId);
        return  idList;
    }

    @Override
    public List<Permission> findByRole(Long roleId) {
        List<Permission> permissions = permissionDao.findByRole(roleId);
        return permissions;
    }

    @Override
    public List<Permission> findPermiMenuByRole(Long roleId, Integer type) {
        List<Permission> permiMenuByRole = permissionDao.findPermiMenuByRole(roleId, type);
        return permiMenuByRole;
    }

    @Override
    public Pageable<Permission> findPage(Integer offset, Integer size) {
        Pageable<Permission> permissionPageable = permissionDao.findPage(offset, size);
        return permissionPageable;
    }

    @Override
    public List<Permission> findListByParent(Long parentId) {
        List<Permission> permissionList = permissionDao.findListByParent(parentId);
        return permissionList;
    }

    @Override
    public List<Permission> findListByType(Integer type) {
        List<Permission> permissions = permissionDao.finfListByType(type);
        return permissions;
    }

    @Override
    public List<Permission> findButtons(Long parentId) {
        List<Permission> permissions = permissionDao.findButtons(parentId);
        return permissions;
    }

}
